//
//  CAIBehaviorEnteringLayer.cpp
//  Monster_Martial_Arts_Version_0
//
//  Created by chiawei.tseng on 13/1/15.
//
//

#include "CAIBehaviorEnteringLayer.h"
#include "CAIManager.h"
#include "CMonsterObject.h"
#include "AnimationDef.h"
#include "cocos2d.h"

using namespace cocos2d;

CAIBehaviorEnteringLayer::CAIBehaviorEnteringLayer():CAIBehavior(),CProcessFunctions(this)
{
    m_nCurrProcess = INVALID;
}

CAIBehaviorEnteringLayer::~CAIBehaviorEnteringLayer()
{
    
}

bool CAIBehaviorEnteringLayer::Init(CAIManager* ptr)
{
    if (ptr)
    {
        m_AiManager = ptr;
        
        Register(ENTERING,&CAIBehaviorEnteringLayer::onProcessEnteringLayer);
        return true;
    }
    
    return false;
}

void CAIBehaviorEnteringLayer::Entering(IStateInput*)
{
    if (isCheckInScene())
    {
        m_nCurrProcess = ENTERING;
        
        MyMaster()->ChangeActiveAnimation(ACTIVE_ANIMATION_ID_MOVE);
    }
    else
    {
        m_ShouldExit = true;
    }
}

void CAIBehaviorEnteringLayer::Update(IStateInput*)
{
    processFun(m_nCurrProcess);
}

void CAIBehaviorEnteringLayer::Leaving(IStateInput*)
{
    m_ShouldExit = false;
}

int CAIBehaviorEnteringLayer::ShouldChange(IStateInput*)
{
    if (m_ShouldExit)
    {
        return BATTLE_OBJECT_STATE_NPC_STANDARD_ATTACK;
    }
    
    return INVALID;
}

void CAIBehaviorEnteringLayer::onProcessEnteringLayer()
{
    if (MyMaster()->GetCurrActiveAnimation() != ACTIVE_ANIMATION_ID_MOVE)
    {
        return;
    }
    
    if(MyMaster()->GetPoint().Step(m_targetPoint, m_targetPoint, MOVELEN))
    {
        m_ShouldExit = true;
    }
}

bool CAIBehaviorEnteringLayer::isCheckInScene()
{
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    
    fPoint point = MyMaster()->GetPoint();
    
    if (point.X < 0)
    {
        m_targetPoint.X = LEFT_COORDINATE_X;
        m_targetPoint.Y = point.Y;
        
        return true;
    }
    else if(point.X >= size.width)
    {
        m_targetPoint.X = RIGHT_COORDINATE_X;
        m_targetPoint.Y = point.Y;
        
        return true;
    }
    
    return false;
}





